summaryrefslogtreecommitdiffstats
path: root/sql/update2_FK.sql
blob: 280abb6e218c91379f56783685758e8faffe1bcd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# pozeni sele, ko je baza updatana vsaj na 10.06.08 !!!


# IDje 0 vstavimo, zato ker se nekje uporabljajo vrednosti 0 pri FK (tudi -1, -2, -3, -4)


INSERT INTO srv_anketa (id, naslov) VALUES ('', 'system');
UPDATE srv_anketa SET id='0' WHERE id=LAST_INSERT_ID(); #pri auto_increment poljih ne mores nastavit 0 pri insertu (ker to pomeni auto_increment), zato je treba z updatom popravit na 0
INSERT INTO srv_anketa (id, naslov) VALUES ('-1', 'system');
INSERT INTO srv_grupa (id, ank_id, naslov) VALUES ('', '0', 'system');
UPDATE srv_grupa SET id='0' WHERE id=LAST_INSERT_ID();
INSERT INTO srv_grupa (id, ank_id, naslov) VALUES ('-1', '0', 'system');
INSERT INTO srv_spremenljivka (id, gru_id, naslov) VALUES ('-1', '0', 'system');
INSERT INTO srv_spremenljivka (id, gru_id, naslov) VALUES ('-2', '0', 'system');
INSERT INTO srv_spremenljivka (id, gru_id, naslov) VALUES ('', '0', 'system');
UPDATE srv_spremenljivka SET id='0' WHERE id=LAST_INSERT_ID();
INSERT INTO srv_vrednost (id, spr_id, naslov) VALUES ('-1', '0', 'system'), ('-2', '0', 'system'), ('-3', '0', 'system'), ('-4', '0', 'system');
INSERT INTO srv_vrednost (id, spr_id, naslov) VALUES ('0', '0', 'system');
UPDATE srv_vrednost SET id='0' WHERE id=LAST_INSERT_ID();
INSERT INTO srv_if (id, label) VALUES ('', 'system');
UPDATE srv_if SET id='0' WHERE id=LAST_INSERT_ID();

#INSERT INTO srv_grid (id) VALUES ('-1'), ('-2'), ('-3'), ('-4');


# POSTAVLJANJE FOREIGN KEY-EV NA BAZO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

# zadeva najprej pobrise vse prazne vrednosti (upam da ne kje prevec) in nato nastavi forein key (FK)

# pri dodajanju prosim upostevaj abecedni vrstni red !!!! Drzi se tudi poimenovanja FK kot je ze drugod! (fk_ime_tabele_ime_polja)






DELETE FROM srv_activity WHERE sid NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_activity ADD CONSTRAINT fk_srv_activity_sid FOREIGN KEY ( sid ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

#DELETE FROM srv_activity WHERE uid NOT IN (SELECT id FROM users);
#ALTER TABLE srv_activity ADD CONSTRAINT fk_srv_activity_uid FOREIGN KEY ( uid ) REFERENCES users( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_alert WHERE ank_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_alert ADD CONSTRAINT fk_srv_alert_ank_id FOREIGN KEY ( ank_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_analysis_archive WHERE sid NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_analysis_archive ADD CONSTRAINT fk_srv_analysis_archive_sid FOREIGN KEY ( sid ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

#DELETE FROM srv_analysis_archive WHERE uid NOT IN (SELECT id FROM users);
#ALTER TABLE srv_analysis_archive ADD CONSTRAINT fk_srv_analysis_archive_uid FOREIGN KEY ( uid ) REFERENCES users( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE srv_anketa ADD CONSTRAINT fk_srv_anketa_folder FOREIGN KEY ( folder ) REFERENCES srv_folder( id ) ON DELETE RESTRICT ON UPDATE CASCADE ;

DELETE FROM srv_branching WHERE ank_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_branching ADD CONSTRAINT fk_srv_branching_ank_id FOREIGN KEY ( ank_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_branching : parent, element_spr, element_if - prevec zakompliciran da bi se dodajal fk (vprasanje kako bi sploh funkcioniral)

DELETE FROM srv_calculation WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_calculation ADD CONSTRAINT fk_srv_calculation_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_calculation WHERE vre_id NOT IN (SELECT id FROM srv_vrednost);
ALTER TABLE srv_calculation ADD CONSTRAINT fk_srv_calculation_vre_id FOREIGN KEY ( vre_id ) REFERENCES srv_vrednost( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_calculation : cnd_id ne morem dodat, ker gre tudi v minus (nekje se idji povezujejo v +, nekje pa v -)

DELETE FROM srv_call_current WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_call_current CHANGE usr_id usr_id INT( 11 ) NOT NULL;        # prej je bil unsigned INT(10) zarad cesar pol ne dela FK
ALTER TABLE srv_call_current ADD CONSTRAINT fk_srv_call_current_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

#DELETE FROM srv_call_current WHERE user_id NOT IN (SELECT id FROM users);
#ALTER TABLE srv_call_current CHANGE user_id user_id INT( 11 ) NOT NULL;
#ALTER TABLE srv_call_current ADD CONSTRAINT fk_srv_call_current_user_id FOREIGN KEY ( user_id ) REFERENCES users( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_call_history WHERE survey_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_call_history CHANGE survey_id survey_id INT( 11 ) NOT NULL;
ALTER TABLE srv_call_history ADD CONSTRAINT fk_srv_call_history_survey_id FOREIGN KEY ( survey_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_call_history WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_call_history CHANGE usr_id usr_id INT( 11 ) NOT NULL;
ALTER TABLE srv_call_history ADD CONSTRAINT fk_srv_call_history_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

#DELETE FROM srv_call_history WHERE user_id NOT IN (SELECT id FROM users);
#ALTER TABLE srv_call_history CHANGE user_id user_id INT( 11 ) NOT NULL;
#ALTER TABLE srv_call_history ADD CONSTRAINT fk_srv_call_history_user_id FOREIGN KEY ( user_id ) REFERENCES users( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_call_schedule WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_call_schedule CHANGE usr_id usr_id INT( 11 ) NOT NULL;
ALTER TABLE srv_call_schedule ADD CONSTRAINT fk_srv_call_schedule_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_call_setting WHERE survey_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_call_setting CHANGE survey_id survey_id INT( 11 ) NOT NULL;
ALTER TABLE srv_call_setting ADD CONSTRAINT fk_srv_call_setting_survey_id FOREIGN KEY ( survey_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_condition WHERE if_id NOT IN (SELECT id FROM srv_if);
ALTER TABLE srv_condition ADD CONSTRAINT fk_srv_condition_if_id FOREIGN KEY ( if_id ) REFERENCES srv_if( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_condition WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_condition ADD CONSTRAINT fk_srv_condition_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_condition WHERE vre_id NOT IN (SELECT id FROM srv_vrednost);
ALTER TABLE srv_condition ADD CONSTRAINT fk_srv_condition_vre_id FOREIGN KEY ( vre_id ) REFERENCES srv_vrednost( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_condition_grid WHERE cond_id NOT IN (SELECT id FROM srv_condition);
ALTER TABLE srv_condition_grid ADD CONSTRAINT fk_srv_condition_grid_cond_id FOREIGN KEY ( cond_id ) REFERENCES srv_condition( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# FK more it u bistvu na (spr_id, grd_id)
#DELETE FROM srv_condition_grid WHERE grd_id NOT IN (SELECT id FROM srv_grid);
#ALTER TABLE srv_condition_grid ADD CONSTRAINT fk_srv_condition_grid_grd_id FOREIGN KEY ( grd_id ) REFERENCES srv_grid( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_condition_vre WHERE cond_id NOT IN (SELECT id FROM srv_condition);
ALTER TABLE srv_condition_vre ADD CONSTRAINT fk_srv_condition_vre_cond_id FOREIGN KEY ( cond_id ) REFERENCES srv_condition( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_condition_vre WHERE vre_id NOT IN (SELECT id FROM srv_vrednost);
ALTER TABLE srv_condition_vre ADD CONSTRAINT fk_srv_condition_vre_vre_id FOREIGN KEY ( vre_id ) REFERENCES srv_vrednost( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_checkgrid WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_checkgrid ADD CONSTRAINT fk_srv_data_checkgrid_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_checkgrid WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_checkgrid ADD CONSTRAINT fk_srv_data_checkgrid_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_checkgrid WHERE vre_id NOT IN (SELECT id FROM srv_vrednost);
ALTER TABLE srv_data_checkgrid ADD CONSTRAINT fk_srv_data_checkgrid_vre_id FOREIGN KEY ( vre_id ) REFERENCES srv_vrednost( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_data_checkgrid : grd_id
# FK je (spr_id, grd_id), ampak treba je najprej zrihtat minus vrednosti

DELETE FROM srv_data_glasovanje WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_glasovanje ADD CONSTRAINT fk_srv_data_glasovanje_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_glasovanje WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_glasovanje ADD CONSTRAINT fk_srv_data_glasovanje_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_grid WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_grid ADD CONSTRAINT fk_srv_data_grid_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_grid WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_grid ADD CONSTRAINT fk_srv_data_grid_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# TODO: srv_data_grid : vre_id, grd_id
# FK more it na obe vrednosti, treba je prej zbrisat minus vrednosti
#DELETE FROM srv_data_grid WHERE (grd_id, spr_id) NOT IN (SELECT id, spr_id FROM srv_grid);
#ALTER TABLE srv_data_grid ADD CONSTRAINT fk_srv_data_grid_grd_id FOREIGN KEY ( grd_id ) REFERENCES srv_grid( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_grid_active WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_grid_active ADD CONSTRAINT fk_srv_data_grid_active_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_grid_active WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_grid_active ADD CONSTRAINT fk_srv_data_grid_active_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# TODO: srv_data_grid_active : vre_id, grd_id

DELETE FROM srv_data_imena WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_imena ADD CONSTRAINT fk_srv_data_imena_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_imena WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_imena ADD CONSTRAINT fk_srv_data_imena_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_number WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_number ADD CONSTRAINT fk_srv_data_number_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_number WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_number ADD CONSTRAINT fk_srv_data_number_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_number WHERE vre_id NOT IN (SELECT id FROM srv_vrednost);
ALTER TABLE srv_data_number ADD CONSTRAINT fk_srv_data_number_vre_id FOREIGN KEY ( vre_id ) REFERENCES srv_vrednost( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_rating WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_rating ADD CONSTRAINT fk_srv_data_rating_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_rating WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_rating ADD CONSTRAINT fk_srv_data_rating_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_rating WHERE vre_id NOT IN (SELECT id FROM srv_vrednost);
ALTER TABLE srv_data_rating ADD CONSTRAINT fk_srv_data_rating_vre_id FOREIGN KEY ( vre_id ) REFERENCES srv_vrednost( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_text WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_text ADD CONSTRAINT fk_srv_data_text_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_text WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_text ADD CONSTRAINT fk_srv_data_text_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_data_text : vre_id (komentarji na spremenljivko imajo v vre_id ID spremenljivke!)

DELETE FROM srv_data_textgrid WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_textgrid ADD CONSTRAINT fk_srv_data_textgrid_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_textgrid WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_textgrid ADD CONSTRAINT fk_srv_data_textgrid_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_textgrid WHERE vre_id NOT IN (SELECT id FROM srv_vrednost);
ALTER TABLE srv_data_textgrid ADD CONSTRAINT fk_srv_data_textgrid_vre_id FOREIGN KEY ( vre_id ) REFERENCES srv_vrednost( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# TODO: srv_data_textgrid : grd_id ? 
# FK je (spr_id, grd_id), ampak treba je najprej zrihtat minus vrednosti

DELETE FROM srv_data_vrednost WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_vrednost ADD CONSTRAINT fk_srv_data_vrednost_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_vrednost WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_vrednost ADD CONSTRAINT fk_srv_data_vrednost_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_vrednost WHERE vre_id NOT IN (SELECT id FROM srv_vrednost);
ALTER TABLE srv_data_vrednost ADD CONSTRAINT fk_srv_data_vrednost_vre_id FOREIGN KEY ( vre_id ) REFERENCES srv_vrednost( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_vrednost_active WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_data_vrednost_active ADD CONSTRAINT fk_srv_data_vrednost_active_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_vrednost_active WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_data_vrednost_active ADD CONSTRAINT fk_srv_data_vrednost_active_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_data_vrednost_active WHERE vre_id NOT IN (SELECT id FROM srv_vrednost);
ALTER TABLE srv_data_vrednost_active ADD CONSTRAINT fk_srv_data_vrednost_active_vre_id FOREIGN KEY ( vre_id ) REFERENCES srv_vrednost( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_dostop WHERE ank_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_dostop ADD CONSTRAINT fk_srv_dostop_ank_id FOREIGN KEY ( ank_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

#DELETE FROM srv_dostop WHERE uid NOT IN (SELECT id FROM users);
#ALTER TABLE srv_dostop ADD CONSTRAINT fk_srv_dostop_uid_id FOREIGN KEY ( uid ) REFERENCES users( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_filter_profiles WHERE sid NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_filter_profiles ADD CONSTRAINT fk_srv_filter_profiles_sid FOREIGN KEY ( sid ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# tale ne dela pri vnosih ???
#DELETE FROM srv_filter_profiles WHERE if_id NOT IN (SELECT id FROM srv_if);
#ALTER TABLE srv_filter_profiles ADD CONSTRAINT fk_srv_filter_profiles_if_id FOREIGN KEY ( if_id ) REFERENCES srv_if( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_filter_profiles : uid (lahko je 0)

# srv_folder : creator_uid (lahko je 0)

DELETE FROM srv_glasovanje WHERE ank_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_glasovanje ADD CONSTRAINT fk_srv_glasovanje_ank_id FOREIGN KEY ( ank_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_glasovanje WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_glasovanje ADD CONSTRAINT fk_srv_glasovanje_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_grid WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_grid ADD CONSTRAINT fk_srv_grid_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_grupa WHERE ank_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_grupa ADD CONSTRAINT fk_srv_grupa_ank_id FOREIGN KEY ( ank_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_help nima nic

# srv_if : folder ? se sploh doda? sicer nima nic (ker je parent element razlicnim child tabelam...)

# srv_invitation_profiles : uid (0) ?

DELETE FROM srv_library_anketa WHERE ank_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_library_anketa ADD CONSTRAINT fk_srv_library_anketa_ank_id FOREIGN KEY ( ank_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_library_anketa : uid 0

# srv_library_folder : uid 0

# srv_misc nima nic

# srv_missing_profiles : uid 0

DELETE FROM srv_mising_profiles_values WHERE pid NOT IN (SELECT id FROM srv_mising_profiles);
ALTER TABLE srv_mising_profiles_values ADD CONSTRAINT fk_srv_mising_profiles_values_pid FOREIGN KEY ( pid ) REFERENCES srv_mising_profiles( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_respondents WHERE pid NOT IN (SELECT id FROM srv_respondent_profiles);
ALTER TABLE srv_respondents ADD CONSTRAINT fk_srv_respondents_pid FOREIGN KEY ( pid ) REFERENCES srv_respondent_profiles( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

#DELETE FROM srv_respondent_profiles WHERE uid NOT IN (SELECT id FROM users);
#ALTER TABLE srv_respondent_profiles ADD CONSTRAINT fk_srv_respondent_uid FOREIGN KEY ( uid ) REFERENCES users( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_specialdata_vrednost WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_specialdata_vrednost ADD CONSTRAINT fk_srv_specialdata_vrednost_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_specialdata_vrednost WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_specialdata_vrednost ADD CONSTRAINT fk_srv_specialdata_vrednost_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_specialdata_vrednost WHERE vre_id NOT IN (SELECT id FROM srv_vrednost);
ALTER TABLE srv_specialdata_vrednost ADD CONSTRAINT fk_srv_specialdata_vrednost_vre_id FOREIGN KEY ( vre_id ) REFERENCES srv_vrednost( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_spremenljivka WHERE gru_id NOT IN (SELECT id FROM srv_grupa);
ALTER TABLE srv_spremenljivka ADD CONSTRAINT fk_srv_spremenljivka_gru_id FOREIGN KEY ( gru_id ) REFERENCES srv_grupa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_spremenljivka : folder - a se to doda?

# srv_statistic_profile : uid

# srv_status_casi : uid 0

# srv_status_profile : uid 0

DELETE FROM srv_survey_misc WHERE sid NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_survey_misc ADD CONSTRAINT fk_srv_survey_misc_sid FOREIGN KEY ( sid ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# TODO : srv_sys_filters

DELETE FROM srv_tracking WHERE ank_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_tracking ADD CONSTRAINT fk_srv_tracking_ank_id FOREIGN KEY ( ank_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_tracking : user (uid 0)

DELETE FROM srv_user WHERE ank_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_user ADD CONSTRAINT fk_srv_user_ank_id FOREIGN KEY ( ank_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_user : user_id (uid 0) - to je povezava na sisplet userja

DELETE FROM srv_userbase WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_userbase ADD CONSTRAINT fk_srv_userbase_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

#DELETE FROM srv_userbase WHERE admin_id NOT IN (SELECT id FROM users);
#ALTER TABLE srv_userbase ADD CONSTRAINT fk_srv_userbase_admin_id FOREIGN KEY ( admin_id ) REFERENCES users( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_userbase_invitations nima nic

DELETE FROM srv_userbase_respondents WHERE list_id NOT IN (SELECT id FROM srv_userbase_respondents_lists);
ALTER TABLE srv_userbase_respondents ADD CONSTRAINT fk_srv_userbase_respondents_list_id FOREIGN KEY ( list_id ) REFERENCES srv_userbase_respondents_lists( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_userbase_respondents_lists nima nic

DELETE FROM srv_userbase_setting WHERE ank_id NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_userbase_setting ADD CONSTRAINT fk_srv_userbase_setting_ank_id FOREIGN KEY ( ank_id ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_userstatus WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_userstatus ADD CONSTRAINT fk_srv_userstatus_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_user_grupa WHERE gru_id NOT IN (SELECT id FROM srv_grupa);
ALTER TABLE srv_user_grupa ADD CONSTRAINT fk_srv_user_grupa_gru_id FOREIGN KEY ( gru_id ) REFERENCES srv_grupa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_user_grupa WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_user_grupa ADD CONSTRAINT fk_srv_user_grupa_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_user_grupa_active WHERE gru_id NOT IN (SELECT id FROM srv_grupa);
ALTER TABLE srv_user_grupa_active ADD CONSTRAINT fk_srv_user_grupa_active_gru_id FOREIGN KEY ( gru_id ) REFERENCES srv_grupa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_user_grupa_active WHERE usr_id NOT IN (SELECT id FROM srv_user);
ALTER TABLE srv_user_grupa_active ADD CONSTRAINT fk_srv_user_grupa_active_usr_id FOREIGN KEY ( usr_id ) REFERENCES srv_user( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

#DELETE FROM srv_user_setting WHERE usr_id NOT IN (SELECT id FROM users);
#ALTER TABLE srv_user_setting ADD CONSTRAINT fk_srv_user_setting_usr_id FOREIGN KEY ( usr_id ) REFERENCES users( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_user_setting_for_survey WHERE sid NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_user_setting_for_survey ADD CONSTRAINT fk_srv_user_setting_for_survey_sid FOREIGN KEY ( sid ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_user_setting_for_survey : uid (lahko je 0)

#DELETE FROM srv_user_setting_misc WHERE uid NOT IN (SELECT id FROM users);
#ALTER TABLE srv_user_setting_misc ADD CONSTRAINT fk_srv_user_setting_misc_uid FOREIGN KEY ( uid ) REFERENCES users( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

DELETE FROM srv_variable_profiles WHERE sid NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_variable_profiles ADD CONSTRAINT fk_srv_variable_profiles_sid FOREIGN KEY ( sid ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_variable_profiles : uid (lahko je 0)

DELETE FROM srv_vrednost WHERE spr_id NOT IN (SELECT id FROM srv_spremenljivka);
ALTER TABLE srv_vrednost ADD CONSTRAINT fk_srv_vrednost_spr_id FOREIGN KEY ( spr_id ) REFERENCES srv_spremenljivka( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_vrednost : if_id je referenca na srv_if id, ampak je veckrat, vprasanje ce je kul, ce se nastavi tukaj fk..

DELETE FROM srv_zanka_profiles WHERE sid NOT IN (SELECT id FROM srv_anketa);
ALTER TABLE srv_zanka_profiles ADD CONSTRAINT fk_srv_zanka_profiles_sid FOREIGN KEY ( sid ) REFERENCES srv_anketa( id ) ON DELETE CASCADE ON UPDATE CASCADE ;

# srv_zanka_profiles : uid (lahko je 0)




#################################

# komentarji na zgornje zadeve :

#################################


# reference na tabelo users (!pazi! to NI tabela srv_user - tam je vse ok)
#  - prvi problem je, da se pri vecini tabel uporablja tudi 0 pri FK (resitev bi bila, da se doda userja z IDjem 0 (ne najbolsa) oz. da se popravi vse 0 pri FK na NULL (potrebni popravki v kodo!))
#  - drugi problem je, da je users na www1kasi MyISAM (ne vem zakaj, jaz jo imam na InnoDB)
#  zato se tabele users zaenkrat ne bo referenciralo (zakomentirano tam, kjer ni problem z 0, komentar (uid 0) tam kjer je se ta problem)